package smartclass.present;

import smartclass.dao.StudentDataDao;
import smartclass.dao.TeacherDataDao;
import smartclass.data.ManagerDataBaseHelper;

import com.example.smartclass.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LogInActivity extends Activity {
	static Context mContext;
	public static ManagerDataBaseHelper db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mContext = this;
		db = new ManagerDataBaseHelper(mContext);
		creatDialog();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private Dialog creatDialog() {

		final AlertDialog dialog = new AlertDialog.Builder(mContext).create();
		dialog.show();
		dialog.getWindow().clearFlags(
				WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
						| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
		dialog.setContentView(R.layout.log_in_dialog);

		final EditText userId = (EditText) dialog.findViewById(R.id.edtUseID);
		final EditText password = (EditText) dialog
				.findViewById(R.id.edtPassword);
		TextView tvRegister = (TextView) dialog.findViewById(R.id.tvDangKy);
		Button btnOK = (Button) dialog.findViewById(R.id.btnOk);
		Button btnCancel = (Button) dialog.findViewById(R.id.btnCancel);
		btnOK.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String user = userId.getText().toString();
				String pass = password.getText().toString();
				if (user.isEmpty()) {
					Toast.makeText(mContext, "Please enter user ID",
							Toast.LENGTH_LONG).show();
					userId.requestFocus();
					return;
				}
				if (pass.isEmpty()) {
					Toast.makeText(mContext, "Please enter your password",
							Toast.LENGTH_LONG).show();
					password.requestFocus();
					return;
				}
				TeacherDataDao teacher = TeacherDataDao.getInstance();
				StudentDataDao student = StudentDataDao.getInstance();
				String passTeacher = teacher.getPassword(Integer.parseInt(user));
				String passStudent = student.getPassword(Integer.parseInt(user));
				System.out.println(passStudent+ "passStudent ");
				if (pass.equals(passTeacher)) {
					Intent intent = new Intent(mContext, TeacherActivity.class);
					intent.putExtra("user", user);
					mContext.startActivity(intent);
					((Activity) mContext).finish();
					
				}
				else if (pass.equals(passStudent)) {
					Intent intent = new Intent(mContext, StudentActivity.class);
					intent.putExtra("user", user);
					mContext.startActivity(intent);
					((Activity) mContext).finish();
					return;
				}
				else {
					Toast.makeText(mContext, "Log In is fail!",
							Toast.LENGTH_LONG).show();
				}

			}
		});

		btnCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				((Activity) mContext).finish();
			}
		});
		tvRegister.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mContext, RegisterActivity.class);
				mContext.startActivity(intent);
			}
		});

		return dialog;

	}

}
